import 'package:flutter/material.dart';
import 'package:kasax_demos2/entry/data_posts_test.dart';
import 'package:kasax_demos2/entry/entrys.dart';
import 'package:kasax/utils/img_utils.dart';

class ListViewDemo2 extends StatefulWidget {
  const ListViewDemo2({Key? key}) : super(key: key);

  @override
  _ListViewDemo2State createState() => _ListViewDemo2State();
}

class _ListViewDemo2State extends State<ListViewDemo2> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("ListView2"),),
      body: SafeArea(
        child: Container(
          margin: EdgeInsets.only(top: 15),
          child: ListView.separated(
              itemBuilder: (context, index) {
                final entry = PostsDatas.TEST_POSTS[index];
                return buildCardContainer(entry, context);
              },
              separatorBuilder: (context, index) {
                return Divider();
              },
              itemCount: PostsDatas.TEST_POSTS.length),
        ),
      ),
    );
  }

  Container buildCardContainer(Posts entry, BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: 10.0, right: 10.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ImageUtils.buildRoundImgByClip(entry.imageUrl),
          SizedBox(
            height: 15,
          ),
          Row(
            children: [
              CircleAvatar(
                backgroundColor: Colors.brown.shade800,
                backgroundImage: NetworkImage(entry.imageUrl),
              ),
              SizedBox(
                width: 10,
              ),
              Text(
                entry.author,
                style: Theme.of(context).textTheme.subtitle,
              ),
              Expanded(
                  child: Text(
                entry.title,
                overflow: TextOverflow.ellipsis,
                maxLines: 1,
                textAlign: TextAlign.end,
                style: Theme.of(context).textTheme.bodyText2,
              ))
            ],
          )
        ],
      ),
    );
  }

  Card buildCard(Posts entry) {
    return Card(
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadiusDirectional.circular(12)),
      clipBehavior: Clip.antiAlias,
      // child: Image.network(entry.imageUrl,fit: BoxFit.cover,),
      child: AspectRatio(
        aspectRatio: 16 / 9,
        child: Image.network(
          entry.imageUrl,
          fit: BoxFit.cover,
        ),
      ),
    );
  }

  Container buildContainer1(Posts entry) {
    return Container(
      decoration: BoxDecoration(
          color: Colors.deepPurpleAccent,
          borderRadius: BorderRadius.circular(12.0)),
      child: AspectRatio(
        aspectRatio: 16 / 9,
        child: Image.network(
          entry.imageUrl,
          fit: BoxFit.cover,
        ),
      ),
    );
  }
}
